var express = require('express');
var router = express.Router();
let { uploadFiles } = require("../utils/handleFiles")

//引入服务层和持久层
let { addInsures, getInsures, deleteInsures, updateInsures, searchInsures } = require('../service/insuresService');

/* GET users listing. */
router.get('/getInsures', async function (req, res) {
    let data = await getInsures(req.query)
    res.send(data);
});

//搜索
router.get('/searchInsures', async function (req, res) {
    let data = await searchInsures(req.query)
    res.send(data)
})

//新增
router.post('/addInsures', async function (req, res, next) {
    console.log(req.body)
    let data = await addInsures(req.body)
    res.send(data)
});

//删除
router.post('/deleteInsures', async function (req, res) {
    let data = await deleteInsures(req.body)
    res.send(data)
})

//修改
router.post('/updateInsures', async function (req, res) {
    let data = await updateInsures(req.body)
    res.send(data)
})

//上传图片
router.post('/uploadImages', function (req, res) {
    let images = uploadFiles({
        path: './public/img',
        key: 'file',
        limit: 5000
    });
    images(req, res, (err) => {
        if (err) {
            res.send({
                msg: "图片上传失败",
                code: 0,
            })
        } else {
            console.log(1111)
            res.send({
                msg: "图片上传成功",
                code: 1,
                data: req.files[0].filename
            })
            console.log(req.files[0].filename)
        }
    })
})

module.exports = router;
